<?xml version="1.0" encoding="UTF-8"?> 
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> 
<%@ page import="java.util.*"%> 
<%@ page import="org.apache.juli.logging.Log" %>
<%@ page import="org.apache.juli.logging.LogFactory"%>
<%@ page import = "com.tomato.dao.PMF" %>
<%@ page import = "ebook.server.Member" %>
<%@ page import = "javax.jdo.PersistenceManager" %>
<%@ page import = "javax.jdo.Query" %>
<%@ page import = "java.util.List" %>
<%@ page import = "com.google.appengine.api.datastore.Key" %>
<%@ page import = "com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import = "com.tomato.bean.Ebook" %>
<%@ page import = "com.google.appengine.api.datastore.Blob" %>

<%
	request.setCharacterEncoding("UTF-8");
	final Log log = LogFactory.getLog("");
	String email = request.getParameter("email"); 
	String pass = request.getParameter("pass"); 
	String book = "1";
	String[] bunri = null;
	String author = null;
	String title = null;
	String description = null;
	String ebook = null;
	String stock = null;
	int count= 0;
	Long id = null;
	Key key;
	
	int j = 0;
	int result = -1; 
	int result1 = 0; 
	log.info("여기왔나 1");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	Query query = pm.newQuery(Member.class);
	log.info("여기왔나 2");
	@SuppressWarnings("unchecked")
	List<Member> emp = 	(List<Member>)query.execute();
	for(Member emps : emp){
		if(emps.getEmail().equals(email)){
			if(emps.getPass().equals(pass)){
				result=1;
				log.info("여기왔나 3");
				book = emps.getBookmaker();
				bunri=book.split("@");
				break;
			}else{
				result=0;
				break;
		    }
		}else{
			result=-1;
		}
	}log.info("여기왔나 4");
%>
<prov>
<% 
	for(j = 0; j < bunri.length; j++){
		Ebook e;
		log.info("여기왔나 5");
		key = KeyFactory.createKey(Ebook.class.getSimpleName(), Integer.parseInt(bunri[j]));
		log.info("여기왔나 5.5");
		e = pm.getObjectById(Ebook.class, key);
		log.info("여기왔나 5.8");
		author = e.getAuthor();
		log.info("여기왔나 6");
		stock = e.getStock();
		description = e.getDescription();
		title = e.getTitle();
		id = e.getId();
		count = e.getCount();
		String image = e.getImage().toString();
		
		byte[] a = e.getEbook().getBytes();
		ebook = new String(a,"UTF-8");
		a = null;
		StringBuffer sb = new StringBuffer();  
		   	for(int k = 0; k < ebook.length(); k++){  
		       if(ebook.charAt(k) == '&'){ 
		          sb.append("amp;");  
		       }else if(ebook.charAt(k) == '<'){  
		          sb.append("lt;");
		       }else if(ebook.charAt(k) == '>'){  
		          sb.append("gt;");  
		       }else  
		          sb.append(ebook.charAt(k));  
		    }
		ebook = sb.toString();
		
	log.info("여기왔나 7");
	if(result==0){ 
		result1 = 0; 
	}else if(result ==-1){ 
		result1 = -1; 
	}else{ 
	
	}
	log.info("여기왔나 8");
%> 

<lists> 
<%if(result==0){ %> 
<data> 
<rowid>4</rowid> 
<msg>失敗！間違ったpasswordです</msg> 
</data> 
<%}else if(result == -1){ %> 
<data> 
<rowid>3</rowid> 
<msg>失敗！idがありません</msg> 
</data> 
<%}else{ %> 
<data> 
<rowid>1</rowid> 
<msg>ようこそ！</msg> 
<author><%= author%></author>
<% if(image != null){ %>
<imageurl><%=request.getServerName()%>/e_book?method=show@amp;id=<%=e.getId()%></imageurl>
<% }else {%>
<imageurl>No image</imageurl>
<% } %>
<id><%= id %></id>
<title><%= title %></title>
<description><%= description %></description>
<date><%= stock %></date>
<ebook><%= ebook %></ebook>

<pwd><%=pass%></pwd> 
<email><%=email%></email> 

</data> 
<%} %>
</lists>
<%} %> 
</prov>